import Vue from 'vue'
import Router from 'vue-router'
import routes from './router'
import store from '../store/index'
Vue.use(Router)

//需要添加的代码
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new Router({
  routes
})


router.beforeEach((to, from, next)=>{
  let token = sessionStorage.getItem('Authorization')
  if(to.path == '/login' || to.path == '/register'){
    next();
  }else{
    if (token) next();
    else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
      store.dispatch('clearUserInfomation')
    }
  }
})
export default router